package com.ruoyi.web.controller.cs;

import com.ruoyi.common.annotation.CheckSwitch;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.controller.BasicsController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.ResponsePageInfo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.domain.TRedbag;
import com.ruoyi.cs.domain.vo.TRechargeVo;
import com.ruoyi.cs.domain.vo.TRedbagVo;
import com.ruoyi.cs.service.ITRedbagService;
import com.ruoyi.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 红包信息Controller
 * 
 * @author ruoyi
 * @date 2021-03-25
 */
@Api(tags = "红包信息管理")
@RestController
@RequestMapping("/buff/redbag")
public class TRedbagController extends BasicsController
{
    private static final Logger log = LoggerFactory.getLogger(TRedbagController.class);
    @Autowired
    private ITRedbagService tRedbagService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询红包信息列表
     */
    @ApiOperation("查询红包信息列表")
    @RequestMapping(name="查询红包信息列表",value="listBack",method = RequestMethod.POST)
    @PreAuthorize("@ss.hasPermi('buff:redbag:listBack')")
    public ResponsePageInfo<TRedbag> listBack(@RequestBody TRedbag tRedbag)
    {
        startPage(tRedbag.getPageNum(),tRedbag.getPageSize());
            List<TRedbag> list = tRedbagService.selectTRedbagList(tRedbag);
        return toResponsePageInfo(list);
    }

    /**
     * 红包类型列表
     */
    @ApiOperation("红包类型列表")
    @RequestMapping(name="查询红包信息列表",value="redBagTypeList",method = RequestMethod.GET)
    @PreAuthorize("@ss.hasPermi('buff:redbag:listBack')")
    public Response<Map<String, String>> redBagTypeList()
    {
        return Response.success(Constants.REDBAGTYPEMAP);
    }



//    /**
//     * 导出红包信息列表
//     */
//    @PreAuthorize("@ss.hasPermi('buff:redbag:export')")
//    @Log(title = "红包信息", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    public AjaxResult export(TRedbag tRedbag)
//    {
//        List<TRedbag> list = tRedbagService.selectTRedbagList(tRedbag);
//        ExcelUtil<TRedbag> util = new ExcelUtil<TRedbag>(TRedbag.class);
//        return util.exportExcel(list, "redbag");
//    }

    /**
     * 获取红包信息详细信息
     */
    @ApiOperation("获取红包信息详细信息")
    @RequestMapping(name="获取红包信息详细信息",value="getInfo",method = RequestMethod.GET)
    @PreAuthorize("@ss.hasPermi('buff:redbag:getInfo')")
    @ApiImplicitParam(name = "redbagId",value = "红包id" ,dataType = "String", paramType = "query", required = true)
    public Response<TRedbag> getInfo(String redbagId)
    {
        return Response.success(tRedbagService.selectTRedbagById(redbagId));
    }

    /**
     * 新增红包信息
     */
    @ApiOperation("新增红包信息")
    @RequestMapping(name="新增红包信息",value="add",method = RequestMethod.POST)
    @PreAuthorize("@ss.hasPermi('buff:redbag:add')")
    @Log(title = "红包信息", businessType = BusinessType.INSERT)
    public Response add(@RequestBody TRedbag tRedbag)
    {
        try {
            return tRedbagService.insertTRedbag(tRedbag);
        } catch (Exception e) {
            return Response.fail(e.getMessage());
        }
    }

    /**
     * 修改红包信息
     */
    @ApiOperation("修改红包信息")
    @RequestMapping(name="修改红包信息",value="edit",method = RequestMethod.POST)
    @PreAuthorize("@ss.hasPermi('buff:redbag:edit')")
    @Log(title = "红包信息", businessType = BusinessType.UPDATE)
    public Response edit(@RequestBody TRedbag tRedbag)
    {
        try {
            return tRedbagService.updateTRedbag(tRedbag);
        } catch (Exception e) {
            return Response.fail(e.getMessage());
        }
    }

    /**
     * 删除红包信息
     */
    @ApiOperation("删除红包信息")
    @RequestMapping(name="删除红包信息",value="remove",method = RequestMethod.GET)
    @PreAuthorize("@ss.hasPermi('buff:redbag:remove')")
    @Log(title = "红包信息", businessType = BusinessType.DELETE)
    @ApiImplicitParam(name = "redbagIds",value = "红包ID,多个用,隔开" ,dataType = "String", paramType = "query", required = true)
    public Response<Integer> remove(String redbagIds)
    {
        try {
            if (StringUtils.isNotEmpty(redbagIds)) {
                String[] ids = redbagIds.split(",");
                return toResponse(tRedbagService.deleteTRedbagByIds(ids));
            }else {
                return Response.fail(Constants.FAILCODE,"参数不能为空！");
            }
        } catch (Exception e) {
            return Response.fail(e.getMessage());
        }
    }
    /**
     * 红包领取记录
     */
    @ApiOperation("红包领取记录")
    @RequestMapping(name="红包领取记录",value="receiveRecode",method = RequestMethod.POST)
    @PreAuthorize("@ss.hasPermi('buff:redbag:receiveRecode')")
    public ResponsePageInfo<TRedbagVo> receiveRecode(@RequestBody TRedbagVo tRedbagVo)
    {
        startPage(tRedbagVo.getPageNum(),tRedbagVo.getPageSize());
        List<TRedbagVo> list = tRedbagService.receiveRecode(tRedbagVo);
        return toResponsePageInfo(list);
    }



    /** ------------------------------------------------前台接口--------------------------------------------*/
    /**
     * 查询红包信息
     */
    @ApiOperation("web查询红包信息")
    @RequestMapping(name="查询红包信息",value="common/getRedBagInfo",method = RequestMethod.GET)
    @ApiImplicitParam(name = "redbagCode",value = "红包口令" ,dataType = "String", paramType = "query", required = true)
    public Response<TRedbag> getRedBagInfo(String redbagCode)
    {
        TRedbag tRedbag = tRedbagService.getRedBagInfoByCode(redbagCode);
        return Response.success(tRedbag);
    }

    /**
     * 领取红包
     * @param
     * @param redbagCode
     * @return
     */
    @CheckSwitch
    @ApiOperation("web领取红包")
    @RequestMapping(name="领取红包",value="receiveRedbag",method = RequestMethod.GET)
    @ApiImplicitParam(name = "redbagCode",value = "红包口令" ,dataType = "String", paramType = "query", required = true)
    @Log(title = "领取红包", businessType = BusinessType.INSERT)
    public Response<TRedbag> receiveRedbag(String redbagCode){
        try {
            if (StringUtils.isNotEmpty(redbagCode)){
                LoginUser loginUser = tokenService.getLoginUserWeb(ServletUtils.getRequest());
                SysUser sysUser = loginUser.getUser();
                String userId = sysUser.getLoginUserId();
                return tRedbagService.receiveRedbag(userId, redbagCode);
            }else {
                return Response.fail(Constants.FAILCODE,"请输入红包口令！");
            }
        } catch (Exception e) {
            log.error("领取红包异常:{}",e.getMessage());
            return Response.fail(e.getMessage());
        }
    }
    /**
     * 红包展示列表
     */
    @ApiOperation("web红包展示列表")
    @RequestMapping(name="红包展示列表",value="common/redBagShowList",method = RequestMethod.GET)
    public Response<List<TRedbagVo>> redBagShowList(){
        LoginUser loginUser = tokenService.getLoginUserWeb(ServletUtils.getRequest());
        String userId = null;
        if(loginUser != null){
            SysUser sysUser = loginUser.getUser();
            userId = sysUser.getLoginUserId();
        }
        List<TRedbagVo> tRedbagVos = tRedbagService.selectShowRedbagList(userId);
        return Response.success(tRedbagVos);
    }

}
